<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="buildingForm" :label-width="formLabelWidth" ref="refBuildingForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="楼栋名称" prop="buildingName">
            <el-input v-model="buildingForm.buildingName" placeholder="请输入楼栋名称" maxlength="100"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="地址" prop="address">
            <el-input v-model="buildingForm.address" placeholder="请输入地址" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="楼层层数" prop="floorCount">
            <el-input-number v-model="buildingForm.floorCount" :min="1" :max="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="楼栋户数" prop="roomCount">
            <el-input-number v-model="buildingForm.roomCount" :min="1" :max="1"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import buildingApi from '@/api/buildingApi'
export default {
  name: 'BuildingAdd',
  components: {},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      buildingForm: {
        buildingName: '',
        address: '',
        floorCount: '',
        roomCount: ''
      },
      rules: {
        buildingName: [
          { required: true, message: '请输入楼栋名称', trigger: 'blur' }
        ],
      },
    }
  },
  created () {
    if (this.id) {
      buildingApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.buildingForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.buildingForm = {
        buildingName: '',
        address: '',
        floorCount: '',
        roomCount: ''
      }
      this.$emit('reset-building-add-event')
    },
    onSave () {
      this.$refs.refBuildingForm.validate((valid) => {
        if (valid) {
          buildingApi.save(this.buildingForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-building-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    onBack () {
      this.$emit('close-building-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>